package com.eshop.tools.service.impl;

import java.io.IOException;
import java.util.Iterator;
import com.eshop.utils.FileUtil;
import java.util.ArrayList;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.eshop.common.utils.QueryHelpPlus;
import com.eshop.tools.service.dto.QiniuConfigDto;
import java.util.LinkedHashMap;
import java.util.List;
import com.github.pagehelper.PageInfo;
import java.util.Map;
import org.springframework.data.domain.Pageable;
import com.eshop.tools.service.dto.QiniuQueryCriteria;
import com.eshop.dozer.service.IGenerator;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import com.eshop.tools.service.QiniuConfigService;
import com.eshop.tools.domain.QiniuConfig;
import com.eshop.tools.service.mapper.QiniuConfigMapper;
import com.eshop.common.service.impl.BaseServiceImpl;

@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = { Exception.class })
public class QiniuConfigServiceImpl extends BaseServiceImpl<QiniuConfigMapper, QiniuConfig> implements QiniuConfigService
{
    private final IGenerator generator;
    private final QiniuConfigMapper qiniuConfigMapper;
    
    public Map<String, Object> queryAll(final QiniuQueryCriteria criteria, final Pageable pageable) {
        this.getPage(pageable);
        final PageInfo<QiniuConfig> page = (PageInfo<QiniuConfig>)new PageInfo((List)this.queryAll(criteria));
        final Map<String, Object> map = new LinkedHashMap<String, Object>(2);
        map.put("content", this.generator.convert(page.getList(), (Class)QiniuConfigDto.class));
        map.put("totalElements", page.getTotal());
        return map;
    }
    
    public List<QiniuConfig> queryAll(final QiniuQueryCriteria criteria) {
        return (List<QiniuConfig>)(this.baseMapper).selectList((Wrapper)QueryHelpPlus.getPredicate(QiniuConfig.class, criteria));
    }
    
    public void download(final List<QiniuConfigDto> all, final HttpServletResponse response) throws IOException {
        final List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        for (final QiniuConfigDto qiniuConfig : all) {
            final Map<String, Object> map = new LinkedHashMap<String, Object>();
            map.put("accessKey", qiniuConfig.getAccessKey());
            map.put("Bucket 识别符", qiniuConfig.getBucket());
            map.put("外链域名", qiniuConfig.getHost());
            map.put("secretKey", qiniuConfig.getSecretKey());
            map.put("空间类型", qiniuConfig.getType());
            map.put("机房", qiniuConfig.getZone());
            list.add(map);
        }
        FileUtil.downloadExcel((List)list, response);
    }
    
    public void update(final String type) {
        this.qiniuConfigMapper.updateType(type);
    }
    
    public QiniuConfigServiceImpl(final IGenerator generator, final QiniuConfigMapper qiniuConfigMapper) {
        this.generator = generator;
        this.qiniuConfigMapper = qiniuConfigMapper;
    }
}
